import {
  createRouter,
  createWebHistory
} from "vue-router";
const routes = [{
    path: "/",
    name: "Layout",
    component: () => import("@/layout/index.vue"),
    children: [{
      path: "/",
      name: "Home",
      component: ()=>import("@/views/Home/index.vue")
    }, ],
  },

  {
    path: "/login",
    name: "login",
    component: () =>
      import( /* webpackChunkName: "about" */ "../views/Login.vue"),
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: ()=>import('@/views/404.vue')
}
];

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [{
  path: "/",
  name: "/",
  component: () => import("@/layout/index.vue"),
  meta: {
    title: "后台首页"
  }
}, {
  path: "/goods/list",
  name: "/goods/list",
  component: () => import("@/views/goods/index.vue"),
  meta: {
    title: "商品管理"
  }
}, {
  path: "/category/list",
  name: "/category/list",
  component: () => import("@/views/category/index.vue"),
  meta: {
    title: "分类列表"
  }
}, {
  path: "/user/list",
  name: "/user/list",
  component: () => import("@/views/user/index.vue"),
  meta: {
    title: "用户列表"
  }
}, {
  path: "/order/list",
  name: "/order/list",
  component: () => import("@/views/order/index.vue"),
  meta: {
    title: "订单列表"
  }
}, {
  path: "/comment/list",
  name: "/comment/list",
  component: () => import("@/views/comment/index.vue"),

  meta: {
    title: "评价列表"
  }
}, {
  path: "/image/list",
  name: "/image/list",
  component: () => import("@/views/image/index.vue"),

  meta: {
    title: "图库列表"
  }
}, {
  path: "/notice/list",
  name: "/notice/list",
  component: () => import("@/views/notice/index.vue"),

  meta: {
    title: "公告列表"
  }
}, {
  path: "/setting/base",
  name: "/setting/base",
  component: () => import("@/views/setting/index.vue"),

  meta: {
    title: "配置"
  }
}, {
  path: "/coupon/list",
  name: "/coupon/list",
  component: () => import("@/views/coupon/index.vue"),

  meta: {
    title: "优惠券列表"
  }
}, {
  path: "/manager/list",
  name: "/manager/list", 
  component: () => import("@/views/manager/index.vue"),
  meta: {
    title: "管理员管理"
  }
}, {
  path: "/access/list",
  name: "/access/list",
  component: () => import("@/views/access/index.vue"),
  meta: {
    title: "菜单权限管理"
  }
}, {
  path: "/role/list",
  name: "/role/list",
  component: () => import("@/views/role/index.vue"),
  meta: {
    title: "角色管理"
  }
}, {
  path: "/skus/list",
  name: "/skus/list",
  component: () => import("@/views/skus/index.vue"),
  meta: {
    title: "规格管理"
  }
}, {
  path: "/level/list",
  name: "/level/list",
  component: () => import("@/views/level/index.vue"),
  meta: {
    title: "会员等级"
  }
}, {
  path: "/setting/buy",
  name: "/setting/buy",
  component: () => import("@/views/setting/index.vue"),

  meta: {
    title: "支付设置"
  }
}, {
  path: "/setting/ship",
  name: "/setting/ship",
  component: () => import("@/views/setting/ship.vue"),

  meta: {
    title: "物流设置"
  }
}, {
  path: "/distribution/index",
  name: "/distribution/index",
  component: () => import("@/views/distribution/index.vue"),

  meta: {
    title: "分销员管理"
  }
}, {
  path: "/distribution/setting",
  name: "/distribution/setting",
  component: () => import("@/views/distribution/setting.vue"),

  meta: {
    title: "分销设置"
  }
}]


export const router = createRouter({
  history: createWebHistory(),
  routes,
});

export const addRoutes = (menus) => {

  // 是否有新的路由
  let hasNewRoutes = false
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach(e => {
      // 判断当前item的路径是否等于动态路由的路径
      let item = asyncRoutes.find(o => o.path == e.frontpath)
      // 如果路径匹配 并且当前路由没有这个路径 进行添加 hasRoute判断路径
      if (item && !router.hasRoute(item.path)) {
        // addRoute(路由的名字，匹配的路由)
        router.addRoute("Layout", item)
        hasNewRoutes = true
      }
      // 如果有子路由进行递归
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child)
      }
    })
  }
  findAndAddRoutesByMenus(menus)

  return hasNewRoutes
}